package com.arvato.minipro.controller.admin.mall;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.arvato.minipro.bean.bean_do.mall.EcBillboardDo;
import com.arvato.minipro.bean.bean_do.mall.CommodityBillboardDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.EcBillboardVo;
import com.arvato.common.ftp.FtpTool;
import com.arvato.minipro.service.mall.EcBillboardService;
import com.arvato.minipro.service.mall.CommodityBillboardService;

/**
 * Describe：后台管理商品广告位的Controller
 *
 * @author smallking
 *
 *  2018年2月8日
 */
@Controller
@RequestMapping(value = "/admin/mall/billboard")
public class AdminBillboardController {
	
	private Logger logger = LoggerFactory.getLogger(AdminBillboardController.class);
	
	@Autowired
	private EcBillboardService billboardService;
	
	@Autowired
	private CommodityBillboardService commodityBillboardService;
	
	@Autowired
	private FtpTool ftpTool;
	
	/**
	 * 查询广告牌列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list", method={RequestMethod.POST})
	public PageData list(){
		Query query = new Query();
		PageData pageData = billboardService.listBillboardByQuery(query, query.getPageNum(), query.getPageSize());
		return pageData;
	}
	
	/**
	 * 根据广告牌ID查询广告牌详情
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result detail(String billboardId){
		Result result = new Result(true);
		EcBillboardVo BillboardVo = billboardService.getBillboardById(billboardId);
		result.setData(BillboardVo);
		return result;
	}
	
	/**
	 * 新增一个广告位
	 * @param billboardDo
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Result insert(EcBillboardDo billboardDo, @RequestParam("file") MultipartFile file){
		Result result = new Result(false);
		billboardDo.setCreateDate(new Date());
		// 存储在本地
		// FileInfo fileInfo = FileUtil.saveFile(file);
		// 存储在远程服务器上
		String fileName = ftpTool.uploadFile(file);
		if(fileName == null) {
			logger.info("图片上传失败");
			result.setMsg("图片上传失败");
			return result;
		}
		billboardDo.setImgPath(fileName);
		try {
			int row = billboardService.insert(billboardDo);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改一个广告位
	 * @param billboardDo
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result update(EcBillboardDo billboardDo, @RequestParam("file") MultipartFile file){
		Result result = new Result(false);
		// 存储在本地
		/*FileInfo fileInfo = FileUtil.saveFile(file);
		if(fileInfo != null){
			billboardDo.setImgPath(fileInfo.getFilePath() + "&" + fileInfo.getFileName());
		}*/
		// 存储在远程服务器上
		if(file != null) {
			String fileName = ftpTool.uploadFile(file);
			if(fileName != null) {
				billboardDo.setImgPath(fileName);
			}
		}
		try {
			int row = billboardService.update(billboardDo);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 删除一个广告位
	 * @param BillboardName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String billboardId){
		Result result = new Result(false);
		try {
			int row = billboardService.delete(billboardId);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 给广告位绑定商品
	 * @param BillboardName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/bind", method = {RequestMethod.POST})
	public Result bind(String commodityId, String billboardId){
		Result result = new Result(false);
		CommodityBillboardDo cb = new CommodityBillboardDo();
		try {
			cb.setCommodityId(commodityId);
			cb.setBillboardId(billboardId);
			int row = commodityBillboardService.insert(cb);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 将商品和广告位解绑
	 * @param BillboardName
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/unbind", method = {RequestMethod.POST})
	public Result unbind(String commodityId){
		Result result = new Result(false);
		Query query = new Query();
		query.put("commodityId", commodityId);
		try {
			int row = commodityBillboardService.delete(query);
			if(row == 1){
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
}
